package ru.braim.weather_app.utils.oms_service;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import ru.braim.weather_app.models.PointOfInterest;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class OMSService {

    public static final String OVERPASS_API = "http://overpass-api.de/api/interpreter";
    public static final String NOMINATIM_API = "https://nominatim.openstreetmap.org";

    public static List<PointOfInterest> getPointsOfInterest(String url) {

        String jsonString = "";

        try {
            List<PointOfInterest> pointOfInterests = new ArrayList<>();
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(OVERPASS_API + "?" + url))
                    .build();
            jsonString = client.send(request, HttpResponse.BodyHandlers.ofString()).body();
            Object obj = new JSONParser().parse(jsonString);
            JSONObject jo = (JSONObject) obj;
            JSONArray elements = (JSONArray)jo.get("elements");


            Iterator phonesItr = elements.iterator();

            while (phonesItr.hasNext()) {
                JSONObject point = (JSONObject) phonesItr.next();
                JSONObject tags = (JSONObject) new JSONParser().parse(point.get("tags").toString());
                System.out.println(tags.get("amenity") + " " + tags.get("name"));
            }

            return null;

        } catch (Exception e) {
            return null;
        }

    }


}
